package com.usdt.web.controller.cp.activity;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.usdt.common.core.domain.OssPolicyResult;
import com.usdt.common.core.domain.R;
import com.usdt.common.core.domain.entity.SysFile;
import com.usdt.system.domain.req.DownloadFileVo;
import com.usdt.system.domain.resp.OssFileVo;
import com.usdt.system.service.cp.IOssService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;


/**
 * @Auther: light
 * @Date: 2018/6/5 10:56
 * @Description:
 */
@RestController
@RequestMapping("/oss")
@Tag(name = "文件上传管理")
//@Anonymous
public class OssController {

    private static Logger logger = LoggerFactory.getLogger(OssController.class);

    @Autowired
    private IOssService ossService;
    
    @Operation(summary ="oss上传签名生成")
	@GetMapping("/getUploadPolicy")
	public R<OssPolicyResult> policy() {
		OssPolicyResult result = ossService.policy();
		return R.ok(result);
	}

    @Operation(summary ="上传回调")
    @PostMapping("/uploadCallback")
    public R<SysFile> callback(@RequestBody @Valid SysFile sysFile) {
        return R.ok(ossService.uploadCallback(sysFile));
    }

    @Operation(summary ="文件下载签证(适用于需要指定文件名的情况)")
    @GetMapping("/downloadPolicy")
    public R<String> downloadPolicy2(@Validated @ModelAttribute DownloadFileVo request) {
        logger.info("要下载的文件:" + request);
        return R.ok(ossService.getDownloadUrl(request.getFileKey(), request.getFileName(),request.getIsPreview()));
    }

    @Operation(summary ="查询文件（包含下载签证)")
    @PostMapping("/queryFilesWithPolicy")
    public R<List<OssFileVo>> queryFileInfoWithPolicy(@NotNull @RequestBody List<String> keys) {
        return R.ok(ossService.queryFilesWithUrl(keys));
    }

    @Operation(summary ="查询文件（包含下载签证)")
    @PostMapping("/queryFilesWithPolicy2")
    public R<List<OssFileVo>> queryFileInfoWithPolicy2(@NotNull @RequestBody List<DownloadFileVo> fileVos) {
        return R.ok(ossService.getDownloadUrls(fileVos));
    }


    @SuppressWarnings("static-access")
    @Operation(summary ="单文件url重定向下载")
    @GetMapping("/download")
    public Object download(@RequestParam String key,
                           @RequestParam(required = false) String fileName,
                           HttpServletResponse response) throws IOException {
        String url = ossService.getDownloadUrl(key, fileName);
        response.sendRedirect(url);
        return R.ok();
    }


}